<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/head.jsp"%>
<script type="text/javascript" src="asserts/plugins/easyui/plugins/jquery-easyui-datagridview/datagrid-detailview.js"></script>
<body>

<table style="width: 100%; height: 100%">
    <tr>
        <td style="width: 60%; vertical-align: top;">
<form id="form1" name="form1" method="post" action="">
    <div id="qp" class="easyui-panel" collapsible="true" style="padding:10px;">


        <span style="margin-left:20px;">住户名:</span>
        <input class="easyui-validatebox myinput" type="text" name="queryName"  maxlength="10" id="queryName" style="width: 100px"></input>

        <span style="margin-left:20px;">住户电话:</span>
        <input class="easyui-validatebox myinput" type="text" name="queryContact"  maxlength="10" id="queryContact" style="width: 100px"></input>

        <span style="margin-left:20px;">地址:</span>
        <input class="easyui-validatebox myinput" type="text" name="queryAddress"   id="queryAddress" style="width: 200px"></input>

        <a  class="easyui-linkbutton myLink" iconCls="icon-ok" style="margin-left:20px;"
            onclick="queryDataGrid()">查询 </a>

        <a  class="easyui-linkbutton myLink" iconCls="icon-undo" style="margin-left:10px;"
            onclick="restDataGrid()">重置 </a>
    </div>
    <table id="datagrid"></table>
       </form>
        </td>
    </tr>
</table>

<!--添加住户dialog-->
<div id="addHouseholdDialog">
    <form name="householdAdd" id="householdAdd">
        <table cellpadding="5" style="margin-left:35px">
            <tr>
                <td>住户名称:&nbsp;<input name="addName" id="addName"  class="easyui-validatebox" style="width:246px;height:30px" required="true"></td>
            </tr>
            <tr>
                <td>住户电话:&nbsp;<input name="addContact" id="addContact" class="easyui-validatebox" style="width:146px;height:30px" required="true"
                                      validType="mobile"></td>
            </tr>
            <tr>
                <td>住户地址:&nbsp;<input name="addAddress" id="addAddress" style="width:246px;height:30px"></td>
            </tr>
        </table>
    </form>
</div>

<!--添加住户dialog-->
<div id="sendNoticeDialog">
    <form name="noticeSend" id="noticeSend">
        <table cellpadding="5" style="margin-left:35px">
            <tr>
                <td>通知内容:&nbsp;
                    <textarea id="content" name="content" class="easyui-validatebox"
                              style="width:246px;height:100px" required="true"></textarea>
            </tr>

        </table>

    </form>
</div>

</body>
<script type="text/javascript" charset="UTF-8">

    function queryDataGrid(){
       var param = $('#form1').serializeJson(true);
       dataGrid.datagrid('load', param);

    }

    function restDataGrid(){
        $('#form1').form("reset");
        queryDataGrid();
    }

    var dataGrid =  $('#datagrid').datagrid({
        url:'community/household/query',
        fit: false, //datagrid自适应宽高,
        toolbar : [
            <%--<%if (SysAuthUtil.ifBtn(request, "roleAdd")) {%>--%>
            {id:'btnadd',text:'增加',iconCls:'icon-add',handler:function(rowData){showAdd();}},
            '-',{id:'btnremove',text:'删除',iconCls:'icon-remove',handler:function(){removeBatch();}}
            ,'-',{id:'btnsend',text:'批量发送短信',iconCls:'icon-add',handler:function(){sendSmsBatch();}	}
             <%--  <%}%> --%>
            <%--
              '-',{id:'btnremove',text:'删除',iconCls:'icon-remove',handler:function(){removeBatch();}}
              ,'-',{id:'btndisable',text:'禁用',iconCls:'icon-remove',handler:function(){disableBatch();}	}
            ,'-',{id:'btnenabl',text:'启用',iconCls:'icon-add',handler:function(){enableBatch();}}--%>
           //,'-',{id:'btnrefresh',text:'刷新',iconCls:'icon-reload',handler:function(){$('#roleDataGrid').datagrid('reload');}}
        ],
        idField : 'id',singleSelect:false,
        pageSize:50,
        pageList: [50,100,500,1000,5000],//可以设置每页记录条数的列表
        frozenColumns : [ [ {
            field : 'ck',
            checkbox : true
        } ] ],
        loadFilter : function(data) {
            if (data.status == 0) {
                var total = 0;
                var rows = "";// 当为[]会出现空的两行
                var push = { "total" : total,"rows" : rows};
                $.messager.alert('提示:', '远程服务调用失败', 'error');
                return push;
            } else {
                return data;
            }

        },
        columns:[[
            {field:'id',hidden:true},
            {field:'householdName',title:'住户名称',align:'center',width:150},
            {field:'householdContact',title:'住户电话',align:'center',width:100},
            {field:'address',title:'住户地址',align:'center',width:300},
            {field:'createTime',title:'创建时间 ',align:'center'},
        ]],
        title:'住户记录',
        pagination:true,
});

    function showAdd(){
        $('#addHouseholdDialog').dialog('open');
    }

    /**
     * 新增注册人弹出框
     */
    $('#addHouseholdDialog').dialog({
        title: '新增住户',
        iconCls: "icon-add",
        collapsible: true,
        minimizable: true,
        maximizable: true,
        resizable: true,
        width: 400,
        height: 250,
        modal: true,
        closed:true,
        buttons:[{
            text:'确定',
            handler:function(){
                var valid = $('#householdAdd').form('validate');
                if (valid) {
                    var data = $('#householdAdd').serializeJson(true);
                    load();
                    postAjax({
                        url: 'community/household/addhousehold',
                        data: data,
                        callback: function (result) {
                            disLoad();
                            if (result.status == 1) {
                                $.messager.alert("提示信息", (result && result.msg)?result.msg:'操作成功', 'info', function () {
                                    $('#householdAdd').form('reset');
                                    $('#addHouseholdDialog').dialog('close');
                                    dataGrid.datagrid('reload');
                                });
                            }else{
                                $.messager.alert('提示信息', (result && result.msg)?result.msg:'操作失败', function(r){
                                });
                            }
                        },
                    });
                }
            },
        },{
            text:'取消',
            handler:function(){
                $('#householdAdd').form('reset');
                $('#addHouseholdDialog').dialog('close');
            },
        },
        ],
    });



    function sendSmsBatch() {
        var rows = $('#datagrid').datagrid('getSelections');
        if (rows.length == 0) {
            $.messager.alert('提示', "请选择待操作的记录", 'info');
        } else {
            $('#sendNoticeDialog').dialog('open');
        }
    }
    /**
     * 发送物业通知弹出框
     */
    $('#sendNoticeDialog').dialog({
        title: '发送物业通知',
        iconCls: "icon-add",
        collapsible: true,
        minimizable: true,
        maximizable: true,
        resizable: true,
        width: 400,
        height: 250,
        modal: true,
        closed:true,
        buttons:[{
            text:'确定',
            handler:function(){
                var valid = $('#noticeSend').form('validate');
                if(!valid){
                    return;
                }
                var rows = $('#datagrid').datagrid('getSelections');
                var data = {
                    "ids":getSelectionIds(rows),
                    "content":$("#content").val()
                }
                load();
                postAjax({
                    url: 'community/household/sendnotice',
                    data: data,
                    callback: function (result) {
                        disLoad();
                        if (result.status == 1) {
                            $.messager.alert("提示信息", (result && result.msg) ? result.msg : '操作成功', 'info', function () {
                                dataGrid.datagrid('getSelections').length = 0;
                            });
                        } else {
                            if(result.data != null) {
                                var total = result.data.total;
                                var success = result.data.successNum;
                                var fail = result.data.failNum;
                                var msg = "发送总数：" + total ;
                                $.messager.alert('提示信息', msg);
                            }else{
                                $.messager.alert('提示信息', result.msg,"error");
                            }
                        }
                        $('#noticeSend').form('reset');
                        $('#sendNoticeDialog').dialog('close');
                    },
                });
            },
        },{
            text:'取消',
            handler:function(){
                $('#noticeSend').form('reset');
                $('#sendNoticeDialog').dialog('close');
            },
        },
        ],
    });

    //批量删除
    function removeBatch() {
        var rows = $('#datagrid').datagrid('getSelections');
        if (rows.length == 0) {
            $.messager.alert('提示', "请选择待操作的记录", 'info');
        } else {
            $.messager.confirm('提示', "确定要删除选中的记录吗？（此操作无法恢复.）", function(r) {
                if (r) {
                    var param = getSelections(rows);
                    load();
                    postAjax({
                        url: 'community/household/delhouseholds'+param,
                        data: "",
                        callback: function (result) {
                            disLoad();
                            if (result.status == 1) {
                                $.messager.alert("提示信息",(result && result.msg)?result.msg:'操作成功', 'info', function() {
                                    dataGrid.datagrid('reload');
                                    dataGrid.datagrid('getSelections').length=0;
                                });
                            }else{
                                $.messager.alert('提示信息', (result && result.msg)?result.msg:'操作失败', function(r){
                                });
                            }

                        },
                    });
                }
            });
        }
    }

    function getSelections(rows) {
        var ids = [];
        for ( var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        return "?ids=" + ids.join(',');
    }

    function getSelectionIds(rows) {
        var ids = [];
        for ( var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }
        return ids.join(",");
    }

</script>
</html>